<template>
    <div>
        <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
            <el-form-item label="品牌名" prop="brandName">
                <el-input v-model="form.brandName"></el-input>
            </el-form-item>
            <el-form-item label="会员logo" prop="vipPicture">
                <el-upload class="avatar-uploader"
                    action="https://localhost:46873/api/app/smart-hotel-app-vIPCard/v-iPUpload" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="form.vipPicture" :src="'https://localhost:46873/VIP/' + form.vipPicture" class="avatar"
                        style="width: 300px; height: 150px;">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="会员卡标题" prop="vipTitle">
                <el-input v-model="form.vipTitle"></el-input>
            </el-form-item>
            <el-form-item label="会员有效期" prop="vipTime">
                <el-input v-model="form.vipTime"></el-input>
            </el-form-item>
            <el-form-item label="特权介绍" prop="privilegeDeclaration">
                <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.privilegeDeclaration">
                </el-input>
            </el-form-item>
            <el-form-item label="使用须知" prop="noticeForUse">
                <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.noticeForUse">
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="UpdateVIPCard('form')">保存</el-button>
                <el-button @click="resetForm('form')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import { Find, Update } from "@/api/VIP";
export default {
    data() {
        return {
            form: {
                id: '',
                brandName: '',
                vipPicture: '',
                vipTitle: '',
                vipTime: '',
                privilegeDeclaration: '',
                noticeForUse: ''
            },
            rules: {
                brandName: [
                    { required: true, message: '品牌名不能为空', trigger: 'blur' },
                ],
                vipTitle: [
                    { required: true, message: '会员卡标题不能为空', trigger: 'blur' },
                ],
                vipTime: [
                    { required: true, message: '有效期不能为空', trigger: 'blur' },
                ],
                privilegeDeclaration: [
                    { required: true, message: '特权介绍不能为空', trigger: 'blur' },
                ],
                noticeForUse: [
                    { required: true, message: '使用须知不能为空', trigger: 'blur' },
                ]
            },

        }
    },
    methods: {
        handleAvatarSuccess(res, file) {
            this.form.vipPicture = res;
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        FindVIPCard(Id) {
            Find(Id).then(res => {
                this.form = res.data;
            })
        },
        UpdateVIPCard(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    Update(this.form).then(res => {
                        if (res) {
                            this.$message.success("修改成功");
                            location.reload();
                        } else {
                            this.$message.error("修改失败");
                            return;
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    created() {
        this.form.id = window.localStorage.getItem("VIPId");
        console.log(this.form.id)
        this.FindVIPCard(this.form.id);
    }
}
</script>
<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>